#include "rda5807_task.h"
#include "rda5807.h"
#include "bsp_key.h"

QueueHandle_t rda5807_xQueue;

typedef struct
{
	uint8_t mode;
	int frequency;
	uint8_t dir;




}__rda5807_task_data;
__rda5807_task_data rda5807_task_data;

void rda5807_task_seek(int dir)
{
	static __rda5807_task_data data;
	BaseType_t xStatus;
	data.mode = 0;
	data.dir = dir;
	xStatus = xQueueSendToBack( rda5807_xQueue, &data, 0 );
	if( xStatus == pdPASS )
	{
		rda5807_task_debug("发送搜台成功");
	}
}
void rda5807_task_set_frequency(int frequency)
{
	static __rda5807_task_data data;
	BaseType_t xStatus;
	data.mode = 1;
	data.frequency = frequency;
	xStatus = xQueueSendToBack( rda5807_xQueue, &data, 0 );
	if( xStatus == pdPASS )
	{
		rda5807_task_debug("发送设置频率成功");
	}
}
void rda5807_task_set_off(void)
{
	static __rda5807_task_data data;
	BaseType_t xStatus;
	data.mode = 2;
	xStatus = xQueueSendToBack( rda5807_xQueue, &data, 0 );
	if( xStatus == pdPASS )
	{
		rda5807_task_debug("发送关闭收音机成功");
	}
}
void rda5807_task_set_on(void)
{
	static __rda5807_task_data data;
	BaseType_t xStatus;
	data.mode = 3;
	xStatus = xQueueSendToBack( rda5807_xQueue, &data, 0 );
	if( xStatus == pdPASS )
	{
		rda5807_task_debug("发送打开收音机成功");
	}
}
void rda5807_task_start_search(void)
{

}

static void rda5807_task(void *pvParameters)
{
	static uint32_t i = 0;
    TickType_t tick;
	BaseType_t status = 0;
    tick = xTaskGetTickCount();
	esp_task_wdt_add(NULL);
    while(1)
    {

		esp_task_wdt_reset();
		status = xQueueReceive( rda5807_xQueue, &rda5807_task_data, pdMS_TO_TICKS(10));
		tick = xTaskGetTickCount();
		if(status == pdPASS)
		{
			rda5807_task_debug("-------------开始执行操作-----------------");
			//rda5807_enable(1);
			//rda5807_mute_set(0);

			if(rda5807_task_data.mode == 3)
			{
				//rda5807_enable(1);
				//system_set_radio_mute(0);
				rda5807_init();
				system_set_spk_switch(SPK_SWITCH_ON);
				system_audio_select(AUDIO_SELECT_RADIO);

				rda5807_task_debug("打开收音机");
			}else
			if(rda5807_task_data.mode == 2)
			{
				system_set_spk_switch(SPK_SWITCH_OFF);
				//system_set_radio_mute(1);
				//rda5807_enable(0);
				rda5807_task_debug("关闭收音机");
			}else
			if(rda5807_task_data.mode == 1)
			{

				rda5807_freq_set(rda5807_task_data.frequency);  //频率设置

				i=0;
				while(rda5807_freq_set_get_sta()==0)
				{
					i++;
					if(i>=3)
					{
						rda5807_task_debug("设置频率失败");
						break;
					}
					vTaskDelayUntil(&tick, pdMS_TO_TICKS(1000));
					esp_task_wdt_reset();
				}
				if(i<=3)
				{
					rda5807_task_debug("设置频率成功");
				}

				rda5807_task_debug("设置频率结束:%d",system_get_radio_frequency());
			}else
			if(rda5807_task_data.mode ==0 )
			{
				system_set_spk_switch(SPK_SWITCH_OFF);

				if(rda5807_task_data.dir == 0)
				{
					rda5807_seek_direction(0);

				}else
				if(rda5807_task_data.dir == 1)
				{
					rda5807_seek_direction(1);
				}

				rda5807_seek_channel_no_block();
				i=0;
				while(1)
				{
					i++;
					if(i>=3)
					{
						rda5807_task_debug("搜台结束");
						break;
					}
					rda5807_task_debug("搜台中:%d",i);
					vTaskDelayUntil(&tick, pdMS_TO_TICKS(1000));
					esp_task_wdt_reset();
				}
				if(rda5807_seek_wait())
				{
					rda5807_task_debug("搜台成功");
				}else
				{
					rda5807_task_debug("搜台失败");
				}
				system_set_spk_switch(SPK_SWITCH_ON);
				system_set_radio_search_status(true);
				rda5807_task_debug("搜台结束:%d",system_get_radio_frequency());
			}
			rda5807_task_debug("-------------结束执行操作-----------------");
		}
    }
}


esp_err_t rda5807_task_init(void)
{
	rda5807_task_debug("初始化...");


	rda5807_xQueue = xQueueCreate( 1, sizeof(__rda5807_task_data) );
	xQueueReceive(rda5807_xQueue, &rda5807_task_data,0);

    xTaskCreate(rda5807_task, "rda5807 task", 1024*2, NULL, 2, NULL);
	rda5807_task_debug("初始化成功");
	return ESP_OK;
}
